<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医院名称" prop="hospitalName">
        <el-input
          v-model="queryParams.hospitalName"
          placeholder="请输入医院名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医院等级" prop="hospitalLevel">
        <el-select
          v-model="queryParams.hospitalLevel"
          placeholder="请选择医院等级"
        >
          <el-option
            v-for="item in hospitalLevelOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="全国综合排名" prop="nationalRank">
        <el-input
          v-model="queryParams.nationalRank"
          placeholder="请输入全国综合排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否支持医保" prop="insurnace">
        <el-select
          v-model="queryParams.insurnace"
          placeholder="是否支持医保"
        >
          <el-option
            v-for="item in insurnaceOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="医院类型" prop="hospitalType">
        <el-select
          v-model="queryParams.hospitalType"
          placeholder="请选择医院类型"
        >
        <el-option
          v-for="item in hospitalTypeOptions"
          :key="item.dictValue"
          :label="item.dictLabel"
          :value="item.dictValue"
        />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="hospitalAddr">
        <el-input
          v-model="queryParams.hospitalAddr"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生数量" prop="dockerNum">
        <el-input
          v-model="queryParams.dockerNum"
          placeholder="请输入医生数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除 0否 1 是" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入是否删除 0否 1 是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:hospitalRoom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hospitalRoom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hospitalRoom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:hospitalRoom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hospitalRoomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医院id" align="center" prop="hospitalId" />
      <el-table-column label="医院名称" align="center" prop="hospitalName" />
      <el-table-column label="医院等级" align="center" prop="hospitalLevel" >
        <template slot-scope="scope">
          <span v-if="scope.row.hospitalLevel==0">三甲</span>
          <span v-if="scope.row.hospitalLevel==1">二甲</span>
          <span v-if="scope.row.hospitalLevel==2">一甲</span>
        </template>
      </el-table-column>
      <el-table-column label="全国综合排名" align="center" prop="nationalRank" />
      <el-table-column label="是否支持医保" align="center" prop="insurnace" >
        <template slot-scope="scope">
          <span v-if="scope.row.insurnace==1">支持医保</span>
          <span v-if="scope.row.insurnace==2">不支持医保</span>
        </template>
      </el-table-column>
      <el-table-column label="医院类型" align="center" prop="hospitalType">
        <template slot-scope="scope">
          <span v-if="scope.row.hospitalType==0">综合医院</span>
          <span v-if="scope.row.hospitalType==1">肿瘤医院</span>
          <span v-if="scope.row.hospitalType==2">口腔医院</span>
          <span v-if="scope.row.hospitalType==3">妇产医院</span>
          <span v-if="scope.row.hospitalType==4">中医医院</span>
          <span v-if="scope.row.hospitalType==5">精神病医院</span>
          <span v-if="scope.row.hospitalType==6">传染病医院</span>
          <span v-if="scope.row.hospitalType==7">儿童医院</span>
          <span v-if="scope.row.hospitalType==8">康复医院</span>
          <span v-if="scope.row.hospitalType==9">五官医院</span>
          <span v-if="scope.row.hospitalType==10">其他医院</span>
        </template>
      </el-table-column>

      <el-table-column label="地址" align="center" prop="hospitalAddr" />
      <el-table-column label="医生数量" align="center" prop="dockerNum" />
      <el-table-column label="医院介绍" align="center" prop="hospitalIntroduction" />
      <el-table-column label="图片" align="center" prop="hospitalPicture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.hospitalPicture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="医院状态" align="center" prop="hospitalStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:hospitalRoom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:hospitalRoom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医院对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医院名称" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="医院等级" prop="hospitalLevel">
          <el-select
            v-model="queryParams.hospitalLevel"
            placeholder="请选择医院等级"
            @change="handleHospitalLevelChange"
          >
            <el-option
              v-for="item in hospitalLevelOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="全国综合排名" prop="nationalRank">
          <el-input v-model="form.nationalRank" placeholder="请输入全国综合排名" />
        </el-form-item>
        <el-form-item label="是否支持医保" prop="insurnace">
          <el-select
            v-model="queryParams.insurnace"
            placeholder="是否支持医保"
            @change="handleInsurnaceChange"
          >
            <el-option
              v-for="item in insurnaceOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="医院类型" prop="hospitalType">
          <el-select
            v-model="queryParams.hospitalType"
            placeholder="请选择医院类型"
            @change="handleHospitalTypeChange"
          >
          <el-option
            v-for="item in hospitalTypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="hospitalAddr">
          <el-input v-model="form.hospitalAddr" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="医生数量" prop="dockerNum">
          <el-input v-model="form.dockerNum" placeholder="请输入医生数量" />
        </el-form-item>
        <el-form-item label="医院介绍">
          <editor v-model="form.hospitalIntroduction" :min-height="192"/>
        </el-form-item>
        <el-form-item label="图片" prop="hospitalPicture">
          <image-upload v-model="form.hospitalPicture"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHospitalRoom, getHospitalRoom, delHospitalRoom, addHospitalRoom, updateHospitalRoom } from "@/api/system/hospitalRoom";

export default {
  name: "HospitalRoom",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医院表格数据
      hospitalRoomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
      pageNum: 1,
        pageSize: 10,
        hospitalName: null,
        hospitalLevel: null,
        nationalRank: null,
        insurnace: null,
        hospitalType: null,
        hospitalAddr: null,
        dockerNum: null,
        hospitalIntroduction: null,
        hospitalPicture: null,
        hospitalStatus: null,
        isDel: null
    },
    // 表单参数
    form: {
      hospitalType: null,
      classGrade: '',
    },
    // 表单校验
    rules: {
    },
      /** 医院类型表数据 */
      hospitalTypeOptions: [],
      /** 医院等级表数据 */
      hospitalLevelOptions: [],
      /** 是否支持医保 */
      insurnaceOptions:[],
  };
  },
  created() {
    this.getList();
    /** 查询医院类型字典表 */
    this.getDicts("hospital_type").then(response => {
      this.hospitalTypeOptions = response.data;
    });
    /** 查询医院等级字典表 */
    this.getDicts("hospital_level").then(response => {
      this.hospitalLevelOptions = response.data;
    });
    /** 查询是否支持医保字典表 */
    this.getDicts("insurnace").then(response => {
      this.insurnaceOptions = response.data;
    })
  },
  methods: {
    /** 查询医院列表 */
    getList() {
      this.loading = true;
      listHospitalRoom(this.queryParams).then(response => {
        this.hospitalRoomList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
     /** 当选择的医院类型变化时，找到对应的标签并更新 */
    handleHospitalTypeChange(value) {
      const selected = this.hospitalTypeOptions.find(
        option => option.dictValue === value
      );
      if (selected) {
        // console.log("111111选中的医院类型：" + selected.dictValue)
        this.form.hospitalType = selected.dictValue;
      }
    },

    /** 当选择的医院类型变化时，找到对应的标签并更新*/
    handleInsurnaceChange(value){
      const selected = this.insurnaceOptions.find(
        option => option.dictValue === value
      );
      if (selected) {
        // console.log("222222选中的医保状态：" + selected.dictValue)
        this.form.insurnace = selected.dictValue;
      }
    },
    /**  当选择的医院等级变化时，找到对应的标签并更新*/
    handleHospitalLevelChange(value){
      const selected = this.hospitalLevelOptions.find(
        option => option.dictValue === value
      );
      if (selected) {
        console.log("333333选中的医院等级：" + selected.dictValue)
        this.form.hospitalLevel = selected.dictValue;
      }
    },
    //取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hospitalId: null,
        hospitalName: null,
        hospitalLevel: null,
        nationalRank: null,
        insurnace: null,
        hospitalType: null,
        hospitalAddr: null,
        dockerNum: null,
        hospitalIntroduction: null,
        hospitalPicture: null,
        hospitalStatus: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hospitalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加医院";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hospitalId = row.hospitalId || this.ids
      getHospitalRoom(hospitalId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改医院";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hospitalId != null) {
            updateHospitalRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHospitalRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hospitalIds = row.hospitalId || this.ids;
      this.$modal.confirm('是否确认删除医院编号为"' + hospitalIds + '"的数据项？').then(function() {
        return delHospitalRoom(hospitalIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/hospitalRoom/export', {
        ...this.queryParams
      }, `hospitalRoom_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
